/*
 * @Name:
 * @Descripttion:
 * @Author: 无痕
 * @Date: 2020-05-21 10:15:27
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-07-01 17:36:39
 */

// 对象取值
export function getObjValue (obj, keyStr, defultValue = '') {
  if (obj && typeof obj === 'object' && keyStr !== '') {
    const keyList = String(keyStr).trim().split('.');
    let value = obj;
    keyList.every(key => {
      value = value[key];
      if (typeof value === 'undefined') {
        value = defultValue;
        return false;
      }
      return true;
    });
    return value;
  }
  return defultValue;
}

// toast
export function toast (obj, cb) {
  const params = Object.assign({
    title: '',
    icon: 'none',
    duration: 1500,
    mask: true
  }, (obj && typeof obj === 'object') ? obj : { title: obj });
  uni.showToast(params);
  typeof cb === 'function' && setTimeout(cb, params.duration);
}

// 根据value获取list中匹配项的label值
export function getLabelByValue (list, value, attr) {
  attr = Object.assign({ label: 'label', value: 'value' }, attr);
  let result = '';
  if (value || value === 0) {
    list.some(item => {
      if (item[attr.value] == value) {
        result = item[attr.label];
        return true;
      }
    });
  }
  return result;
}

// 获取url参数
export function getUrlParam (name, url) {
  const reg = new RegExp('(\\?|&|^)' + name + '=([^&]*)(&|$)');
  const r = (url || window.location.search).match(reg);
  return r ? unescape(r[2]) : undefined;
}
